package com.ybwh.springboot2.leafid.controller;

import com.xiaoju.uemc.tinyid.client.utils.TinyId;
import com.ybwh.springboot2.common.RespData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;


@Api(tags = "tinyid生成器")
@Controller
@RequestMapping("/")
public class LeafidController {


    @ApiOperation(value = "分布式id", notes = "")
    @ResponseBody
    @GetMapping("/getid")
    public RespData<Long> getid() {

        // 按需批量分布式自增ID
//        List< Long > ids =  TinyId. nextId( " test " , 10 );

        // 获取单个分布式自增ID
        Long id = TinyId.nextId(" test ");

        return RespData.successResp(id);
    }


    @ApiOperation(value = "上传文件", notes = "")
    @ResponseBody
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public RespData upload(@ApiParam(value = "上传文件", required = true) @RequestParam("file") MultipartFile file)
            throws IllegalStateException, IOException {


        file.transferTo(new File("D:/tmp/" + file.getOriginalFilename()));


        return RespData.successResp(null);

    }


}
